﻿//Include("Mz");

/**
 * @fileoverview 在指定的DOM位置插入HTML，此方法代替IE里的insertAdjacentHTML和innerHTML
 * @author meizz
 * @version 20090423
 *
 * @name Mz.insertHTML
 * @namespace Mz.insertHTML
 * @param {HTMLElement} element 指定的DOM节点
 * @param {String} html 需要被插入的HTML字符
 * @param {String} where 指定的插入地点，目前支持四个点：beforeBegin afterBegin beforeEnd afterEnd
 */

Mz.insertHTML = function (element, html, where) {
    if (typeof element == "string") {
        element = document.getElementById(element);
    }

    if (typeof element != "object" || !element || !element.tagName) {
        return;
    }

    if (element.insertAdjacentHTML) {
        element.insertAdjacentHTML(where, html);
    }
    else if (typeof HTMLElement != "undefined" && !window.opera) {
        var e = element.ownerDocument.createRange();
        e.setStartBefore(element);
        e = e.createContextualFragment(html);

        switch(where.toLowerCase()){
            case 'beforebegin':
                element.parentNode.insertBefore(e, element);
                break;
            case 'afterbegin':
                element.insertBefore(e, element.firstChild);
                break;
            case 'beforeend':
                element.appendChild(e);
                break;
            case 'afterend':
                if(!element.nextSibling) element.parentNode.appendChild(e);
                else element.parentNode.insertBefore(e,element.nextSibling);
                break;
        }
    }
};
